<?php
/**
 * Customer.php
 *
 * @copyright   2023 thingsworld.com.cn - All Rights Reserved
 * @link        http://thingsworld.com.cn/
 * @author      stiffer.chen <chenl@thingsworld.com.cn>
 * @created     2023-02-14 01:47:07
 * @modified    2023-02-14 01:47:07
 *
 */

namespace App\Models;

use Carbon\Carbon;

class Customer extends BaseModel
{
    protected $table = 'customer';

    protected $fillable = [
        'parent_id',
        'customer_user_id',
        'customer_number',
        'name',
        'short_name',
        'customer_type_name',
        'entry_type_name',
        'tag_name',
        'industry_name',
        'safe_level',
        'source',
        'department_id',
        'department_name',
        'owner_id',
        'owner_name',
        'country_id',
        'country_name',
        'province_id',
        'province_name',
        'city_id',
        'city_name',
        'county_id',
        'county_name',
        'address',
        'status',
        'create_user_id',
        'service_limit',
        'contact_at'
    ];

    protected $appends = ['detail_address', 'zone_codes', 'province_code', 'city_code', 'county_code', 'create_time_format', 'update_time_format'];

    public function getDetailAddressAttribute()
    {
        return $this->province_name . $this->city_name . $this->county_name . $this->address;
    }

    public function getZoneCodesAttribute()
    {
        return [
            $this->province_id,
            $this->city_id,
            $this->county_id
        ];
    }

    public function getProvinceCodeAttribute()
    {
        $province = CityModel::find($this->province_id);
        return $province->citycode ?? '';
    }

    public function getCityCodeAttribute()
    {
        $city = CityModel::find($this->city_id);
        return $city->citycode ?? '';
    }

    public function getCountyCodeAttribute()
    {
        $county = CityModel::find($this->county_id);
        return $county->citycode ?? '';
    }

    public function getCreateTimeFormatAttribute()
    {
        return Carbon::parse($this->create_time)->toDateTimeString();
    }

    public function getUpdateTimeFormatAttribute()
    {
        return Carbon::parse($this->update_time)->toDateTimeString();
    }

    public function scopeByCustomerNumber($query, $number)
    {
        return $query->where('customer_number', $number);
    }

    public function scopeByName($query, $name)
    {
        return $query->where('name', $name);
    }

    public function scopeByShortName($query, $shortName)
    {
        return $query->where('short_name', $shortName);
    }

    public function user()
    {
        return $this->hasOne(\App\Models\Customer\User::class, 'id', 'customer_user_id');
    }

    public function users()
    {
        return $this->hasMany(\App\Models\Customer\User::class);
    }

    public function department()
    {
        return $this->belongsTo(DeptModel::class);
    }

    public function owner()
    {
        return $this->belongsTo(\App\Models\Customer\User::class, 'owner_id');
    }
    public function parent()
    {
        return $this->belongsTo(Customer::class, 'parent_id');
    }

    public function create_user()
    {
        return $this->belongsTo(UserModel::class, 'create_user_id');
    }

    public function products()
    {
        return $this->hasMany(Product::class);
    }
}